package com.example.practice2023.service;

import com.example.practice2023.mapper.AdminMapper;
import com.example.practice2023.pojo.Admin;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import org.apache.shiro.subject.Subject;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: UserService
 * Package: com.example.practice2023.service
 * Description:
 *
 * @Author hyx
 * @Create 2023/7/3 0:16
 * @Version 1.0
 */
@Service
public class AdminService {
    @Resource
    private AdminMapper adminMapper;

    public Admin getAdmin(String adminName) {
        return adminMapper.selectByUserName(adminName);
    }

    public Map<String, Object> selectByUserName(String adminName, String adminPass) {
        Map<String, Object> map = new HashMap<>();
        Subject currentUser = SecurityUtils.getSubject();                                     // shiro权限认证主体对象
        if (!currentUser.isAuthenticated()) {
            UsernamePasswordToken upToken = new UsernamePasswordToken(adminName, adminPass);    // shiro权限认证类型
            upToken.setRememberMe(true);                                                     // 用户登录时效性
            try {
                currentUser.login(upToken);    // 调用realm认证用户权限
                map.put("code", 200);
                map.put("msg", "登录成功");
                map.put("username", adminName);
            } catch (IncorrectCredentialsException ice) {
                System.out.println("用户名/密码不匹配！");
                map.put("code", 50001);
                map.put("msg", "用户名/密码不匹配！");
            } catch (LockedAccountException lae) {
                System.out.println("账户已被冻结！");
                map.put("code", 50002);
                map.put("msg", "账户已被冻结！");
            } catch (UnknownAccountException uae) {
                System.out.println("账户不存在");
                map.put("code", 50003);
                map.put("msg", "账户不存在！");
            } catch (AuthenticationException ae) {
                System.out.println("额外的错误");
                System.out.println(ae.getMessage());
                map.put("code", 50004);
                map.put("msg", "额外的错误！");
            }
        }
        return map;
    }

    // 注册时，进行shiro加密，返回加密后的结果，如果在加入shiro之前，存在用户密码不是此方式加密的，那么将无法登录
    // 使用用户名作为盐值
    private String md5(String username, String password){
        String hashAlgorithmName = "MD5";                   // 加密方式
        ByteSource salt = ByteSource.Util.bytes(username);  // 以账号作为盐值salt
        int hashIterations = 11;                            // 加密11次
        return new SimpleHash(hashAlgorithmName, password, salt, hashIterations).toString();
    }

//    public static void main(String[] args) {
//        AdminService adminService = new AdminService();
//        String md5 = adminService.md5("wang", String.valueOf(123456));
//        System.out.println(md5);
//    }
}
